package com.hugesoft.jxgs.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.hugesoft.jxgs.R;
import com.hugesoft.jxgs.activity.ListActivityBusInfo.LvAdapter;
import com.hugesoft.jxgs.activity.base.BaseActivity;
import com.hugesoft.logic.managers.LogicManager;
import com.hugesoft.logic.managers.common.Constans;
import com.hugesoft.logic.module.Bus;
import com.hugesoft.logic.module.DriverLicence;
import com.hugesoft.logic.module.Traffic;

public class ListActivityTrafficInfo extends BaseActivity {
	ListView mListView;
	private static List<Traffic> mTrafficList = new ArrayList<Traffic>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activitylist);
		((TextView) findViewById(R.id.titleTextView)).setText("危化品车辆历史纪录");
		findViewById(R.id.backImg).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		Intent intent = getIntent();
		mTrafficList = LogicManager.trafficManager().loadtrafficinfo(
				intent.getExtras().get(Constans.TAGPLATENUMBER).toString());

		mListView = (ListView) findViewById(R.id.lvrst);

		mListView.setAdapter(new LvAdapter(ListActivityTrafficInfo.this));

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Traffic traffic = mTrafficList.get(arg2);
				Intent intent = new Intent(ListActivityTrafficInfo.this,
						TrafficActivity.class);
				intent.putExtra(Constans.TAGTRAFFICID, traffic.getId());
				startActivityForResult(intent, Constans.REQUESTCODE_NEWTRAFFIC);
			}
		});

	}

	static class LvAdapter extends BaseAdapter implements SectionIndexer {
		private Context mContext;

		@SuppressWarnings("unchecked")
		public LvAdapter(Context mContext) {
			this.mContext = mContext;
		}

		@Override
		public int getCount() {
			return mTrafficList.size();
		}

		@Override
		public Object getItem(int position) {
			return mTrafficList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final Traffic trafficinfo = mTrafficList.get(position);
			final DriverLicence licence = LogicManager.driverLicenceManager()
					.loadlicenceinfo(trafficinfo.getDriverid());
			ViewHolder viewHolder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.activitylistitemtrafficinfo, null);
				viewHolder = new ViewHolder();
				viewHolder.tvDriverLicenceNo = (TextView) convertView
						.findViewById(R.id.textDriverLicenceNo);
				viewHolder.tvRecordTime = (TextView) convertView
						.findViewById(R.id.textRecordTime);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			viewHolder.tvDriverLicenceNo.setText(licence.getDrivername());
			viewHolder.tvRecordTime.setText(trafficinfo.getCheckdate()
					.toLocaleString());
			return convertView;
		}

		static class ViewHolder {
			TextView tvDriverLicenceNo;// 司机姓名
			TextView tvRecordTime;// 采集时间
		}

		@Override
		public int getSectionForPosition(int position) {
			return 0;
		}

		@Override
		public Object[] getSections() {
			return null;
		}

		@Override
		public int getPositionForSection(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == Constans.REQUESTCODE_NEWBUS && data != null) {
			String platenumber = data.getStringExtra(Constans.TAGPLATENUMBER);
			mTrafficList = LogicManager.trafficManager().loadtrafficinfo(platenumber);
			((BaseAdapter) mListView.getAdapter()).notifyDataSetChanged();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
